export type ShareableResource = {
    id: number
    type: "song" | "album" | "playlist" | "mv" | "artist"
    title: string
    cover: string
    artist?: string
}

export function shareResource(resource: ShareableResource) {
    // 将资源信息存储在 sessionStorage 中，以便分享页面可以获取
    sessionStorage.setItem("sharedResource", JSON.stringify(resource))

    // 跳转到分享页面
    window.location.href = "/community/share"
}

export function useSharedResource() {
    // 从 sessionStorage 中获取分享的资源信息
    const getResource = (): ShareableResource | null => {
        if (typeof window === "undefined") return null

        const resourceStr = sessionStorage.getItem("sharedResource")
        if (!resourceStr) return null

        try {
            return JSON.parse(resourceStr)
        } catch (e) {
            console.error("Failed to parse shared resource", e)
            return null
        }
    }

    // 清除分享的资源信息
    const clearResource = () => {
        if (typeof window === "undefined") return
        sessionStorage.removeItem("sharedResource")
    }

    return { getResource, clearResource }
}

